package org.zebra.web.annotation.validation;

import cn.hutool.core.lang.Validator;
import cn.hutool.core.text.CharSequenceUtil;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * @author	zhanghongbin
 */
public class EnglishValidator implements ConstraintValidator<English, String> {

    private boolean notNull;

    @Override
    public void initialize(English constraintAnnotation) {
        this.notNull = constraintAnnotation.notNull();
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        if (CharSequenceUtil.isNotBlank(value)) {
            return Validator.isWord(value);
        }

        if (notNull) {
            return false;
        }

        return true;
    }
}
